package com.panshi.controller.search;


import com.google.code.kaptcha.Producer;
import com.panshi.api.IAboutApi;
import com.panshi.api.IMessagApi;
import com.panshi.utils.GetSessionUser;
import com.panshi.vo.UserVO;
import com.panshi.vo.search.Paging;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

@Controller
@Slf4j
public class AboutController {

    @Autowired
    IAboutApi aboutApi ;

    @Autowired
    private IMessagApi messagApi; //站内消息api

    public Map<String, Object> getUserCookie(Map<String, Object> model) {
        //从session里面拿到用户
        UserVO loginUser = GetSessionUser.getSessionUser();
        //登录过了
        if (loginUser != null) {
/*           //登录的用户id存入redis中
            Jedis jedis = redisUtils.getJedis();
            jedis.set("loginUserId", loginUser.getU_id() + "", "NX", "EX", 300);*/
            model.put("loginUser", loginUser);
            //获取该用户的站内消息数量
            int mesgCount = messagApi.inquireSum(loginUser.getU_id()).getData();
            model.put("mesgCount", mesgCount);
        }
        return model;
    }

    @Resource
    Producer kaptchaProducer;

    @GetMapping("/about/link")
    public String link(Map<String,Object> model){
        log.info("进来了++++++++++++link");
        model.put("type",aboutApi.link());

        return "about/link";
    }

    @GetMapping("/about/me")
    public String me(Map<String,Object> model){
        log.info("进来了++++++++++++me");
        model.put("type",aboutApi.me());

        return "about/me";
    }

    @GetMapping("/about/map")
    public String map(Map<String,Object> model){
        log.info("进来了++++++++++++map");
        model.put("type",aboutApi.map());

        return "about/map";
    }

    @GetMapping("/about/leave")
    public String leave(Map<String,Object> model){
        log.info("进来了++++++++++++leave");
        model.put("type",aboutApi.leave());

        return "about/leave";
    }

    @GetMapping("/about/zhaoping")
    public String zhaoping(Map<String,Object> model){
        log.info("进来了++++++++++++zhaoping");
        model.put("type",aboutApi.zhaoping());

        return "about/zhaoping";
    }

    @GetMapping("/about/agree")
    public String agree(Map<String,Object> model){
        log.info("进来了++++++++++++agree");
        model.put("type",aboutApi.agree());

        return "about/agree";
    }

    @GetMapping("/about/privacy")
    public String privacy(Map<String,Object> model){
        log.info("进来了++++++++++++privacy");
        model.put("type",aboutApi.privacy());

        return "about/privacy";
    }

    //控制层调用验证码
    @GetMapping(path = "/common/vcode")
    @ResponseBody
    public void getKaptcha(HttpServletResponse response, HttpSession session) {
        // 生成验证码
        String text = kaptchaProducer.createText();
        BufferedImage image = kaptchaProducer.createImage(text);
        System.out.println("kaptcha===="+session);
        System.out.println("sessionid =="+session.getId());
        //将验证码存入session中
        session.setAttribute("kaptcha", text);

        System.out.println("图形验证码：" + text);
        // 将突图片输出给浏览器
        response.setContentType("image/png");
        try {
            OutputStream os = response.getOutputStream();
            ImageIO.write(image, "png", os);
        } catch (IOException e) {
            System.out.println("响应验证码失败:" + e.getMessage());
        }
    }


    @RequestMapping("/search")
    public String search(Map<String,Object> model, String kw,Integer p){
        log.info("第一步进入webController            "+kw+"==="+p);
        Paging<String> pa = new Paging<>();
        pa.setRows(kw);
        if(p != null){
            pa.setCurrentPage(p);
        }
        model = getUserCookie(model);
        model.put("pa",aboutApi.search(pa));
        model.put("findSou",aboutApi.findSou());
        model.put("kw",kw);
        return "search";
    }

}